//
//  TimeingSynchronization.m
//  Estate
//
//  Created by heyong on 2016/12/2.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "TimeingSynchronization.h"

@implementation TimeingSynchronization
- (NSMutableArray *)proids{
    if(!_proids){
        _proids=[NSMutableArray array];
    }
    return _proids;
}

- (NSMutableArray *)userArr{
    if(_userArr==nil){
        _userArr=[NSMutableArray array];
    }
    return _userArr;
}

- (NSMutableArray *)workRecordArr{
    if(_workRecordArr==nil){
        _workRecordArr=[NSMutableArray array];
    }
    return _workRecordArr;
}

- (NSMutableArray *)workDiaryArr{
    if(!_workDiaryArr){
        _workDiaryArr=[NSMutableArray array];
    }
    return _workDiaryArr;
}

-(NSMutableArray *)attachArr{
    if(_attachArr==nil){
        _attachArr=[NSMutableArray array];
    }
    return _attachArr;
}

- (NSMutableArray *)workLogArr{
    if(!_workLogArr){
        _workLogArr=[NSMutableArray array];
    }
    return _workLogArr;
}

- (NSMutableArray *)unitArr{
    if(!_unitArr){
        _unitArr=[NSMutableArray array];
    }
    return _unitArr;
}
- (NSMutableArray *)proUnitArr{
    if(!_proUnitArr){
        _proUnitArr=[NSMutableArray array];
    }
    return _proUnitArr;
}

- (NSMutableArray *)proUsers{
    if(_proUsers==nil){
        _proUsers=[NSMutableArray array];
    }
    return _proUsers;
}

-(NSMutableArray *)inspectProcessArr{
    if(!_inspectProcessArr){
        _inspectProcessArr=[NSMutableArray array];
    }
    return _inspectProcessArr;
}

- (NSMutableArray *)inspectProcrssRecordArr{
    if(!_inspectProcrssRecordArr){
        _inspectProcrssRecordArr=[NSMutableArray array];
    }
    return _inspectProcrssRecordArr;
}

-(NSMutableArray *)inspectItemArr{
    if(!_inspectItemArr){
        _inspectItemArr=[NSMutableArray array];
    }
    return _inspectItemArr;
}

- (NSMutableArray *)inspectUserItemArr{
    if(!_inspectUserItemArr){
        _inspectUserItemArr=[NSMutableArray array];
    }
    return _inspectUserItemArr;
}

- (NSMutableArray *)inspectTotalRecordArr{
    if(!_inspectTotalRecordArr){
        _inspectTotalRecordArr=[NSMutableArray array];
    }
    return _inspectTotalRecordArr;
}

-(NSMutableArray *)inspectGroupArr{
    if(!_inspectGroupArr){
        _inspectGroupArr=[NSMutableArray array];
    }
    return _inspectGroupArr;
}

-(NSMutableArray *)inspectGroupUserArr{
    if(!_inspectGroupUserArr){
        _inspectGroupUserArr=[NSMutableArray array];
    }
    return _inspectGroupUserArr;
}
- (NSMutableArray *)advArr{
    if(!_advArr){
        _advArr=[NSMutableArray array];
    }
    return _advArr;
}

- (NSMutableArray *)systemArr{
    if(!_systemArr){
        _systemArr=[NSMutableArray array];
    }
    return _systemArr;
}

- (NSMutableArray *)addressArr{
    if(!_addressArr){
        return _addressArr=[NSMutableArray array];
    }
    return _addressArr;
}

- (NSMutableArray *)projectArr{
    if(!_projectArr){
        _projectArr=[NSMutableArray array];
    }
    return _projectArr;
}
- (NSMutableArray *)projectEngineerSupveyArr{
    if(!_projectEngineerSupveyArr){
        _projectEngineerSupveyArr=[NSMutableArray array];
    }
    return _projectEngineerSupveyArr;
}

-(NSMutableArray *)sampleCodeArr{
    if(!_sampleCodeArr){
        _sampleCodeArr=[NSMutableArray array];
    }
    return _sampleCodeArr;
}
- (NSMutableArray *)processArr{
    if(!_processArr){
        _processArr=[NSMutableArray array];
    }
    return _processArr;
}

- (NSMutableArray *)processRecordArr{
    if(!_processRecordArr){
        _processRecordArr=[NSMutableArray array];
    }
    return _processRecordArr;
}

- (NSMutableArray *)sampleProcessArr{
    if(!_sampleProcessArr){
        _sampleProcessArr=[NSMutableArray array];
    }
    return _sampleProcessArr;
}

-(NSMutableArray *)sampleProcessRecordArr{
    if(!_sampleProcessRecordArr){
        _sampleProcessRecordArr=[NSMutableArray array];
    }
    return _sampleProcessRecordArr;
}

#pragma mark- 是否有数据更新方法

- (void)isSyncLocalData{
    [self.userArr removeAllObjects];
    [self.workRecordArr removeAllObjects];
    [self.workDiaryArr removeAllObjects];
    [self.workLogArr removeAllObjects];
    [self.unitArr removeAllObjects];
    [self.proUsers removeAllObjects];
    [self.attachArr removeAllObjects];
    [self.proUnitArr removeAllObjects];
    [self.advArr removeAllObjects];
    [self.systemArr removeAllObjects];
    [self.inspectProcessArr removeAllObjects];
    [self.inspectProcrssRecordArr removeAllObjects];
    [self.inspectItemArr removeAllObjects];
    [self.inspectUserItemArr removeAllObjects];
    [self.inspectTotalRecordArr removeAllObjects];
    [self.inspectGroupArr removeAllObjects];
    [self.inspectGroupUserArr removeAllObjects];
    [self.addressArr removeAllObjects];
    [self.projectArr removeAllObjects];
    [self.projectEngineerSupveyArr removeAllObjects];
    [self.sampleCodeArr removeAllObjects];
    [self.processArr removeAllObjects];
    [self.processRecordArr removeAllObjects];
    [self.sampleProcessArr removeAllObjects];
    [self.sampleProcessRecordArr removeAllObjects];
    
    [self serverDataIsUpdate];
    
    //判断是否有网
    //    [LSHttpTool checkNetWork:^(NSString *status) {
    //        if(![status isEqualToString:@"0"]){
    //        }else{
    //            DeLog(@"没有网络，不进入校验");
    //        }
    //    }];
    
}

#pragma mark - 查询本地数据表 是否有ADD 或者 MOD数据
- (BOOL)queayLocalDataContainAddOrModByTable:(NSString *)table{
    FMDatabase *db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    NSString *sql=[NSString stringWithFormat:@"SELECT ID,MODIFY_TIME,LOCAL_MODIFY_STATE FROM %@ WHERE LOCAL_MODIFY_STATE IN('ADD','MOD','DEL')",table];
    FMResultSet *resule=[db executeQuery:sql];
    BOOL isExist=NO;
    while ([resule next]) {
        isExist=YES;
    }
    [db close];
    return isExist;
}

#pragma  mark -查询服务器 是否有更改数据
/*
 *  有新数据 返回No 没有返回yes
 */

-(void)serverDataIsUpdate{
    
    group = dispatch_group_create();
    
    [self downloadUser];
    [self downloadWorkRecord];
    [self downloadWorkDiary];
    [self downloadWorkLog];
    [self downloadUnit];
    [self downLoadAdv];
    [self downSystemConfig];
    [self downloadProUnit];
    [self downloadInspectProcess];
    [self downloadInspectItem];
    [self downloadInspectProcessRecord];
    [self downloadInspectUserItem];
    [self downloadInspectTotalRecord];
    [self downloadInspectGroup];
    [self downloadInspectGroupUser];
    
    [self queryProjectAddressFromServe];
    [self queryProjectFromServe];
    
    [self queryProjectProfileFromServe];
    [self queryProcessFromServe];
    [self queryProcessRecordFromServe];
    
    [self querySampleProcessFromServe];
    [self querySampleProcessRecordFromServe];
    
    dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
        
        if(self.userArr.count>0||self.workRecordArr.count>0||self.workDiaryArr.count>0||self.workLogArr.count>0||self.unitArr.count>0||self.proUsers.count>0||self.attachArr.count>0||self.proUnitArr.count>0||self.advArr.count>0||self.systemArr.count>0||self.inspectProcessArr.count>0||self.inspectProcrssRecordArr.count>0||self.inspectItemArr.count>0||self.inspectUserItemArr.count>0||self.inspectTotalRecordArr.count>0||self.inspectGroupArr.count>0||self.inspectGroupUserArr.count>0||self.addressArr.count>0||self.projectArr.count>0||self.projectEngineerSupveyArr.count>0||self.sampleCodeArr.count>0||self.processArr.count>0||self.processRecordArr.count>0||self.sampleProcessArr.count>0||self.sampleProcessRecordArr.count>0){
            self.isNoData=NO;
        }else{
            self.isNoData=YES;
        }
        
        NSLog(@"%@",self.isNoData==0?@"有更新":@"无更新");
        NSDictionary *dic=@{@"isHiden":@(self.isNoData)};
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName:@"indexReloadBtn" object:nil userInfo:dic];
    });
    
}

/*
 *    高能 服务器数据表校验 如需更改请做注释的操作
 */

#pragma  mark -服务器表是否有数据需要更新

- (void)downloadUser{
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    
    NSArray *arr=[LSUtility getIdAndModifyTimeFromTable:USER];
    
    if(arr!=nil){
        
        // 本地数组转换成json
        NSString *json=[LSUtility dictionaryToJson:arr];
        
        [dic setObject:json forKey:@"localdata"];
        
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_USER_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            
            if([code isEqualToString:@"1"]){
                
                for (NSDictionary *mudic in responseObject[@"data"]){
                    if([self serverDataCompareLocalDataTable:USER Data:mudic]){
                        [self.userArr addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"user表查询错误:%@",responseObject);
            }
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
        
    }else{
        NSLog(@"用户表本地无数据");
        dispatch_group_leave(group);
        
    }
}

- (void)downloadWorkRecord{
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *arr=[LSUtility getIdAndModifyTimeFromTable:WORK_RECORD];
    
    if(arr!=nil)
    {
        // 本地数组转换成json
        NSString *jsonArr=[LSUtility dictionaryToJson:arr];
        
        [dic setObject:jsonArr forKey:@"localdata"];
        NSString *role=DEF_PERSISTENT_GET_OBJECT(@"role");
        if (role&&role.length>0) {
            [dic setObject:role forKey:@"userType"];
        }
        dispatch_group_enter(group);
        
        //更新数据请求
        [LSHttpTool Post:URL_WORKRECORD_SELECT parameters:dic success:^(id responseObject) {
            
            NSString *code=[responseObject objectForKey:@"code"];
            
            if([code isEqualToString:@"1"]){
                
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:WORK_RECORD Data:mudic]){
                        [self.workRecordArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"工作记录表错误:%@",responseObject );
                dispatch_group_leave(group);
                
            }
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
    }
    else
    {
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic setObject:[LSUtility parameterDownLoadWorkLogProIds] forKey:@"proIds"];
        NSString *role=DEF_PERSISTENT_GET_OBJECT(@"role");
        if (role&&role.length>0) {
            [dic setObject:role forKey:@"userType"];
        }
        dispatch_group_enter(group);
        
        //更新数据请求
        [LSHttpTool Post:URL_WORKRECORD_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:WORK_RECORD Data:mudic]){
                        [self.workRecordArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"错误404日志表 %@",responseObject);
                dispatch_group_leave(group);
                
            }
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
    }
    
}


- (void)downloadWorkDiary{
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    
    NSArray *arr=[LSUtility getIdAndModifyTimeFromTable:WORK_DIARY];
    
    if(arr!=nil)
    {
        // 本地数组转换成json
        NSString *jsonArr=[LSUtility dictionaryToJson:arr];
        
        [dic setObject:jsonArr forKey:@"localdata"];
        NSString *role=DEF_PERSISTENT_GET_OBJECT(@"role");
        if (role&&role.length>0) {
            [dic setObject:role forKey:@"userType"];
        }

        dispatch_group_enter(group);
        
        //更新数据请求
        [LSHttpTool Post:URL_WORKDIARY_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:WORK_DIARY Data:mudic]){
                        [self.workDiaryArr  addObject:mudic];
                    }
                    
                }
                dispatch_group_leave(group);
                
            }else{
                
                NSLog(@"错误443 日记表：%@",responseObject);
                dispatch_group_leave(group);
                
            }
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
    }
    else
    {
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic setObject:[LSUtility parameterDownLoadWorkLogProIds] forKey:@"proIds"];
        NSString *role=DEF_PERSISTENT_GET_OBJECT(@"role");
        if (role&&role.length>0) {
            [dic setObject:role forKey:@"userType"];
        }

        //更新数据请求
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_WORKDIARY_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:WORK_DIARY Data:mudic]){
                        [self.workDiaryArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"错误日记 465：%@",responseObject );
                dispatch_group_leave(group);
                
            }
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
            
        }];
    }
    
}

- (void)downloadWorkLog{
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    
    NSArray *arr=[LSUtility getIdAndModifyTimeFromTable:WORK_LOG];
    
    if(arr!=nil)
    {
        // 本地数组转换成json
        NSString *jsonArr=[LSUtility dictionaryToJson:arr];
        [dic setObject:jsonArr forKey:@"localdata"];
        [dic setObject:[LSUtility parameterDownLoadWorkLogProIds] forKey:@"proIds"];
        
        if(![dic[@"proIds"] isEqualToString:@""]){
            //更新数据请求
            dispatch_group_enter(group);
            
            [LSHttpTool Post:URL_WORKLOG_SELECT parameters:dic success:^(id responseObject) {
                NSString *code=[responseObject objectForKey:@"code"];
                if([code isEqualToString:@"1"]){
                    //                    NSLog(@"日志返回：%@",responseObject);
                    for (NSDictionary *mudic in responseObject[@"data"]) {
                        //得到服务器返回的字典
                        if([self serverDataCompareLocalDataTable:WORK_LOG Data:mudic]){
                            [self.workLogArr  addObject:mudic];
                        }
                    }
                    dispatch_group_leave(group);
                    
                }else{
                    NSLog(@"错误721-%@",[responseObject objectForKey:@"msg"]);
                    dispatch_group_leave(group);
                }
            } failure:^(NSError *error) {
                dispatch_group_leave(group);
                
            }];
        }else{
            NSLog(@"日志同步巡检不存在项目ID");
            
        }
    }
    else
    {
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic setObject:[LSUtility parameterDownLoadWorkLogProIds] forKey:@"proIds"];
        dispatch_group_enter(group);
        
        //更新数据请求
        [LSHttpTool Post:URL_WORKLOG_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            NSLog(@"responseObject:%@",[responseObject objectForKey:@"data"]);
            if([code isEqualToString:@"1"]){
                NSLog(@"日志返回：%@",responseObject);
                
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:WORK_LOG Data:mudic]){
                        [self.workLogArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
            }else{
                NSLog(@"错误日志 529：%@",responseObject);
                dispatch_group_leave(group);
            }
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
        
    }
}

- (void)downloadUnit{
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    
    NSArray *arr=[LSUtility getIdAndModifyTimeFromTable:UNIT];
    
    if(arr!=nil){
        
        // 本地数组转换成json
        NSString *json=[LSUtility dictionaryToJson:arr];
        
        [dic setObject:json forKey:@"localdata"];
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_UNIT_SELECT parameters:dic success:^(id responseObject) {
            
            NSString *code=[responseObject objectForKey:@"code"];
            
            if([code isEqualToString:@"1"]){
                
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:UNIT Data:mudic]){
                        [self.unitArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                
                NSLog(@"错误单位567：%@",responseObject);
                dispatch_group_leave(group);
                
            }
            
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
        
    }else{
        NSLog(@"本地单位无数据");
        dispatch_group_leave(group);
    }
    
}

- (void)downloadProUsers {
    
    NSArray *arr = [LSUtility getProjectUser:@"proUsers"proIds:self.proids];
    
    if(arr.count>0){
        NSString *jsonStr = [LSUtility dictionaryToJson:arr];
        NSMutableDictionary *parameterDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:jsonStr, @"localdata", nil];
        //        NSLog(@"项目人员入参：%@",parameterDic);
        
        [LSHttpTool Post:URL_PROJECTUSER_SELECT parameters:parameterDic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            if([code isEqualToString:@"1"]) {
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:PROJECT_USER Data:mudic]){
                        [self.proUsers  addObject:mudic];
                    }
                }
                
            }else{
                NSLog(@"项目人员错误601： %@",responseObject);
                
            }
            
        } failure:^(NSError *error) {
            
        }];
        
    }else{
        NSMutableDictionary *parameterDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"", @"localdata", nil];
        NSLog(@"项目人员入参：%@",parameterDic);
        
        [LSHttpTool Post:URL_PROJECTUSER_SELECT parameters:parameterDic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            if([code isEqualToString:@"1"]) {
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:PROJECT_USER Data:mudic]){
                        [self.proUsers  addObject:mudic];
                    }
                }
                
            }else{
                NSLog(@"项目人员错误601： %@",responseObject);
                
            }
            
        } failure:^(NSError *error) {
            
        }];
    }
}

-(void)downLoadAdv{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *arr=[LSUtility getIdAndModifyTimeFromTable:ADVERTISING_PICTURE];
    if(arr!=nil)
    {
        // 本地数组转换成json
        NSString *jsonArr=[LSUtility dictionaryToJson:arr];
        [dic setObject:jsonArr forKey:@"localdata"];
        //更新数据请求
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_ADVERTIING_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:ADVERTISING_PICTURE Data:mudic]){
                        [self.advArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"广告错误633：%@",responseObject);
                dispatch_group_leave(group);
            }
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
        }];
    }
    else
    {
        [dic setObject:@"" forKey:@"localdata"];
        
        //更新数据请求
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_ADVERTIING_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            //            NSLog(@"responseObject:%@",[responseObject objectForKey:@"data"]);
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:ADVERTISING_PICTURE Data:mudic]){
                        [self.advArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"%@",[responseObject objectForKey:@"msg"]);
                dispatch_group_leave(group);
                
            }
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
    }
}

- (void)downSystemConfig{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    
    NSArray *arr=[LSUtility getIdAndModifyTimeFromTable:SYSTEM_CONFIG];
    
    if(arr!=nil)
    {
        // 本地数组转换成json
        NSString *jsonArr=[LSUtility dictionaryToJson:arr];
        [dic setObject:jsonArr forKey:@"localdata"];
        //更新数据请求
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_SYSTEMCONFIG_SELECT parameters:dic success:^(id responseObject) {
            //            NSLog(@"配置responseObject:%@",responseObject[@"msg"]);
            NSString *code=[responseObject objectForKey:@"code"];
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:SYSTEM_CONFIG Data:mudic]){
                        [self.systemArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"系统配置错误686：%@",responseObject);
                dispatch_group_leave(group);
                
            }
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
    }
    else
    {
        [dic setObject:@"" forKey:@"localdata"];
        //更新数据请求
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_SYSTEMCONFIG_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            //NSLog(@"responseObject:%@",[responseObject objectForKey:@"data"]);
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:SYSTEM_CONFIG Data:mudic]){
                        [self.systemArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"系统配置错误707：%@",responseObject);
                dispatch_group_leave(group);
                
            }
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
    }
}

- (void)downloadProUnit{
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    
    //    NSArray *arr = [LSUtility getProjectUser:@"unitCnt"];
    
    NSArray *arr=[LSUtility queryUnitNumByProId:USERID andType:@"unitCnt"];
    
    
    if(arr!=nil){
        
        // 本地数组转换成json
        NSString *json=[LSUtility dictionaryToJson:arr];
        
        [dic setObject:json  forKey:@"localdata"];
        //        NSLog(@"项目单位 入参:%@",dic);
        dispatch_group_enter(group);
        DeLog(@"项目单位入参：%@",dic);
        [LSHttpTool Post:URL_PROJECTUNIT_SELECT parameters:dic success:^(id responseObject) {
            
            NSString *code=[responseObject objectForKey:@"code"];
            
            if([code isEqualToString:@"1"]){
                
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:PROJECT_UNIT Data:mudic]){
                        [self.proUnitArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"项目单位错误740：%@",responseObject);
                dispatch_group_leave(group);
                
            }
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
    }else{
        
        [dic setObject:@"" forKey:@"localdata"];
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_PROJECTUNIT_SELECT parameters:dic success:^(id responseObject) {
            
            NSString *code=[responseObject objectForKey:@"code"];
            
            if([code isEqualToString:@"1"]){
                
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:PROJECT_UNIT Data:mudic]){
                        [self.proUnitArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"项目单位错误766：%@",responseObject);
                dispatch_group_leave(group);
            }
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
    }
    
}

- (void)downloadInspectProcess{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    //本地非add mod数据
    NSMutableArray *arr=[LSUtility getIdAndModifyTimeFromTable:INSPECT_PROCESS];
    
    if(arr!=nil)
    {
        // 本地数组转换成json
        NSString *jsonArr=[LSUtility dictionaryToJson:arr];
        [dic setObject:jsonArr  forKey:@"localdata"];
        //        NSLog(@"服务器工程巡检流程入参:%@",dic);
        dispatch_group_enter(group);
        
        //更新数据请求
        [LSHttpTool Post:URL_INSPECTPROCESS_SELECT parameters:dic success:^(id responseObject) {
            //            NSLog(@"巡检流程记录出参:%@",responseObject);
            NSString *code=[responseObject objectForKey:@"code"];
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:INSPECT_PROCESS Data:mudic]){
                        [self.inspectProcessArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"巡检流程查询错误：%@",responseObject);
                dispatch_group_leave(group);
                
            }
            
        } failure:^(NSError *error) {
            NSLog(@"巡检流程查询请求发生错误");
            dispatch_group_leave(group);
            
        }];
    }
    else
    {
        [dic setObject:@"" forKey:@"localdata"];
        //更新数据请求
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_INSPECTPROCESS_SELECT parameters:dic success:^(id responseObject) {
            NSLog(@"服务器工程巡检流程出参:%@",responseObject);
            NSString *code=[responseObject objectForKey:@"code"];
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:INSPECT_PROCESS Data:mudic]){
                        [self.inspectProcessArr  addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"错误-1113：%@",[responseObject objectForKey:@"msg"]);
                dispatch_group_leave(group);
                
            }
        } failure:^(NSError *error) {
            NSLog(@"巡检流程查询请求发生错误");
            dispatch_group_leave(group);
            
        }];
    }
    
}

- (void)downloadInspectItem{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    
    //本地非add mod数据
    NSMutableArray *arr=[LSUtility getIdAndModifyTimeFromTable:INSPECT_ITEM];
    
    if(arr!=nil)
    {
        
        // 本地数组转换成json
        NSString *jsonArr=[LSUtility dictionaryToJson:arr];
        [dic setObject:jsonArr  forKey:@"localdata"];
        
    }else{
        [dic setObject:@"" forKey:@"localdata"];
        
    }
    
    //更新数据请求
    dispatch_group_enter(group);
    
    [LSHttpTool Post:URL_INSPECTITEM_SELECT parameters:dic success:^(id responseObject) {
        NSString *code=[responseObject objectForKey:@"code"];
        if([code isEqualToString:@"1"]){
            for (NSDictionary *mudic in responseObject[@"data"]) {
                //得到服务器返回的字典
                if([self serverDataCompareLocalDataTable:INSPECT_ITEM Data:mudic]){
                    [self.inspectItemArr  addObject:mudic];
                }
            }
            dispatch_group_leave(group);
            
        }else{
            NSLog(@"错误1467-%@",responseObject);
            dispatch_group_leave(group);
            
        }
        
    } failure:^(NSError *error) {
        dispatch_group_leave(group);
        
    }];
    
}

- (void)downloadInspectProcessRecord {
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    //本地非add mod数据
    NSMutableArray *arr=[LSUtility getIdAndModifyTimeFromTable:INSPECT_PROCESS_RECORD];
    if(arr!=nil)
    {
        // 本地数组转换成json
        NSString *jsonArr=[LSUtility dictionaryToJson:arr];
        [dic setObject:jsonArr forKey:@"localdata"];
        NSString *str=[LSUtility queryInspectProcessIds];
        NSString *inspectProcessIds = [LSUtility strbydan:str];
        if (inspectProcessIds && inspectProcessIds.length>0) {
            [dic setObject:inspectProcessIds forKey:@"inspectProcessIds"];
        }else{
            return ;
        }        //更新数据请求
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_INSPECTPROCESSRECORD_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            //            NSLog(@"记录返回：%@",responseObject);
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:INSPECT_PROCESS_RECORD Data:mudic]){
                        [self.inspectProcrssRecordArr addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"巡检流程记录错误896：%@",responseObject);
                dispatch_group_leave(group);
                
            }
            
        } failure:^(NSError *error) {
            NSLog(@"巡检流程记录查询请求发生错误");
            dispatch_group_leave(group);
            
        }];
    }
    else
    {
        // 本地数组转换成json
        NSString *str=[LSUtility queryInspectProcessIds];
        NSString *inspectProcessIds = [LSUtility strbydan:str];
        if (inspectProcessIds && inspectProcessIds.length>0) {
            [dic setObject:inspectProcessIds forKey:@"inspectProcessIds"];
        }else{
            return ;
        }
        dispatch_group_enter(group);
        
        //更新数据请求
        [LSHttpTool Post:URL_INSPECTPROCESSRECORD_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            //            NSLog(@"记录返回：%@",responseObject);
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:INSPECT_PROCESS_RECORD Data:mudic]){
                        [self.inspectProcrssRecordArr addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"巡检流程错误923-%@ \n msg:%@",responseObject ,responseObject[@"msg"]);
                dispatch_group_leave(group);
                
            }
            
        } failure:^(NSError *error) {
            NSLog(@"巡检流程记录查询请求发生错误");
            dispatch_group_leave(group);
            
        }];
        
    }
}

- (void)downloadInspectUserItem{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *arr=[LSUtility getIdAndModifyTimeFromTable:INSPECT_USER_ITEM];
    if(arr!=nil)
    {
        // 本地数组转换成json
        NSString *jsonArr=[LSUtility dictionaryToJson:arr];
        [dic setObject:jsonArr forKey:@"localdata"];
        NSString *str=[LSUtility queryInspectProcessIds];
        NSString *inspectProcessIds = [LSUtility strbydan:str];
        if (inspectProcessIds && inspectProcessIds.length>0) {
            [dic setObject:inspectProcessIds forKey:@"inspectProcessIds"];
        }else{
            return ;
        }        //更新数据请求
        dispatch_group_enter(group);
        [LSHttpTool Post:URL_INSPECTUSERITEM_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            //            NSLog(@"下载用户巡检项：%@",responseObject);
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:INSPECT_USER_ITEM Data:mudic]){
                        [self.inspectUserItemArr addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"巡检用户项错误 957：%@",responseObject);
                dispatch_group_leave(group);
                
            }
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
    }
    else
    {
        // 本地数组转换成json
        NSString *str=[LSUtility queryInspectProcessIds] ;
        NSString *inspectProcessIds = [LSUtility strbydan:str];
        if (inspectProcessIds && inspectProcessIds.length>0) {
            [dic setObject:inspectProcessIds forKey:@"inspectProcessIds"];
        }else{
            return ;
        }        //更新数据请求
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_INSPECTUSERITEM_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:INSPECT_USER_ITEM Data:mudic]){
                        [self.inspectUserItemArr addObject:mudic];
                    }
                    
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"巡检用户项错误 982：%@\n msg:%@",responseObject,responseObject[@"msg"]);
                dispatch_group_leave(group);
            }
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
        }];
    }
}

- (void)downloadInspectTotalRecord{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *arr=[LSUtility getIdAndModifyTimeFromTable:INSPECT_TOTAL_RECORD];
    if(arr!=nil)
    {
        // 本地数组转换成json
        NSString *jsonArr=[LSUtility dictionaryToJson:arr];
        [dic setObject:jsonArr forKey:@"localdata"];
        NSString *processids= [LSUtility queryInspectProcessIds];
        NSString *inspectProcessIds = [LSUtility strbydan:processids];
        if (inspectProcessIds && inspectProcessIds.length>0) {
            [dic setObject:inspectProcessIds forKey:@"inspectProcessIds"];
        }else{
            return ;
        }        //更新数据请求
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_INSPECTTOTALRECORD_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    //得到服务器返回的字典
                    if([self serverDataCompareLocalDataTable:INSPECT_TOTAL_RECORD Data:mudic]){
                        [self.inspectTotalRecordArr addObject:mudic];
                    }
                    
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"错误1756-%@",[responseObject objectForKey:@"msg"]);
                dispatch_group_leave(group);
                
            }
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
    }
    else
    {
        NSLog(@"本地无数据");
    }
}

- (void)downloadInspectGroup{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *arr=[LSUtility getIdAndModifyTimeFromTable:INSPECT_GROUP];
    if(arr!=nil)
    {
        // 本地数组转换成json
        NSString *jsonArr=[LSUtility dictionaryToJson:arr];
        [dic setObject:jsonArr forKey:@"localdata"];
        NSString *str=[LSUtility queryInspectProcessIds] ;
        NSString *inspectProcessIds = [LSUtility strbydan:str];
        if (inspectProcessIds && inspectProcessIds.length>0) {
            [dic setObject:inspectProcessIds forKey:@"inspectProcessIds"];
        }else{
            return ;
        }
        dispatch_group_enter(group);
        
        //更新数据请求
        [LSHttpTool Post:URL_INSPECTGROUP_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    if([self serverDataCompareLocalDataTable:INSPECT_GROUP Data:mudic]){
                        [self.inspectGroupArr addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"错误--%@",[responseObject objectForKey:@"msg"]);
                dispatch_group_leave(group);
                
            }
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
    }
    else
    {
        NSLog(@"本地无数据");
    }
    
}

- (void)downloadInspectGroupUser{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *arr=[LSUtility getIdAndModifyTimeFromTable:INSPECT_GROUP_USER];
    if(arr!=nil)
    {
        // 本地数组转换成json
        NSString *jsonArr=[LSUtility dictionaryToJson:arr];
        [dic setObject:jsonArr forKey:@"localdata"];
        NSString *str=[LSUtility queryInspectProcessIds] ;
        NSString *inspectProcessIds = [LSUtility strbydan:str];
        if (inspectProcessIds && inspectProcessIds.length>0) {
            [dic setObject:inspectProcessIds forKey:@"inspectProcessIds"];
        }else{
            return ;
        }        //更新数据请求
        dispatch_group_enter(group);
        
        [LSHttpTool Post:URL_INSPECTGROUPUSER_SELECT parameters:dic success:^(id responseObject) {
            NSString *code=[responseObject objectForKey:@"code"];
            if([code isEqualToString:@"1"]){
                for (NSDictionary *mudic in responseObject[@"data"]) {
                    if([self serverDataCompareLocalDataTable:INSPECT_GROUP_USER Data:mudic]){
                        [self.inspectGroupUserArr addObject:mudic];
                    }
                }
                dispatch_group_leave(group);
                
            }else{
                NSLog(@"巡检组用户错误-1996:%@",responseObject);
                dispatch_group_leave(group);
                
            }
            
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
        }];
    }
    else
    {
        NSLog(@"本地无数据");
    }
    
}

- (void)queryProjectAddressFromServe {
    
    NSArray *arr = [LSUtility getProjectUser:@"add"proIds:self.proids];
    
    if(arr != nil){
        NSString *jsonStr = [LSUtility dictionaryToJson:arr];
        NSMutableDictionary *parameterDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:jsonStr, @"localdata", nil];
        // NSLog(@"位置入参：%@",parameterDic);
        
        //        NSLog(@"地址入参：%@",parameterDic);
        
        dispatch_group_enter(group);
        // 向服务器查询数据
        [LSHttpTool Post:URL_ADDRESS_SELECT parameters:parameterDic success:^(id responseObject) {
            //        NSLog(@"地址出参：%@",responseObject);
            
            if ([responseObject[@"code"] isEqualToString:@"1"]){
                NSArray *flowRecordArr = responseObject[@"data"];
                for (NSDictionary *dic in flowRecordArr) {
                    if([self serverDataCompareLocalDataTable:ADDRESS Data:dic]){
                        [self.addressArr addObject:dic];
                    }
                }
                dispatch_group_leave(group);
                
            } else {
                NSLog(@"返回项目地址错误:%@",responseObject);
                dispatch_group_leave(group);
                
            }
        } failure:^(NSError *error) {
            dispatch_group_leave(group);
            
            NSLog(@"项目地址请求超时");
        }];
    }else{
        NSLog(@"位置无数据更新");
    }
}

- (void)queryProjectFromServe {
    NSString *json = [self getLocalIDAndModifyTimeFrom:PROJECT];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:json forKey:@"localdata"];
    [dic setObject:DEF_PERSISTENT_GET_OBJECT(@"realname") forKey:@"usernames"];
    dispatch_group_enter(group);
    
    [LSHttpTool Post:URL_PROJECT_SELECT parameters:dic success:^(id responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"1"]){
            //            NSLog(@"返回项目信息正确");
            NSArray *arr = responseObject[@"data"];
            for (NSDictionary *dic in arr) {
                if([self serverDataCompareLocalDataTable:PROJECT Data:dic]){
                    [self.projectArr addObject:dic];
                }
                NSString *ids=dic[@"id"];
                if([LSUtility queryProjectForId:ids]){
                    [self.proids addObject:dic[@"id"]];
                }
            }
            
            dispatch_group_leave(group);
            
            [self downloadProUsers];
        } else {
            NSLog(@"返回项目信息错误:%@",responseObject);
            dispatch_group_leave(group);
            
            [self downloadProUsers];
            
        }
    } failure:^(NSError *error) {
        //NSLog(@"项目信息请求超时");
        dispatch_group_leave(group);
        [self downloadProUsers];
        
        
    }];
}


- (void)queryProjectProfileFromServe {
    // 从本地查项目ID
    NSArray *projectIDArr = [self getLocalID:@"ID" fromTable:PROJECT];
    NSMutableArray *parameterArr = [NSMutableArray array];
    for (int i = 0; i < projectIDArr.count; i++) {
        // 获取项目概况ID和最后修改时间
        NSString *jsonStr = [self getLocalIDAndModifyTimeFrom:PRO_ENGINEER_SURVEY where:@"PRO_ID" equal:projectIDArr[i]];
        //没有相关的项目概览 list传空
        NSDictionary * dic;
        if(!jsonStr){
            dic= @{@"proId":projectIDArr[i], @"list":@"[]"};
        }else{
            dic=  @{@"proId":projectIDArr[i], @"list":jsonStr};
        }
        [parameterArr addObject:dic];
    }
    // 把参数转化为JSON字符串
    NSString *parameterStr = [LSUtility toJSONData:parameterArr];
    // 拼接到可变字典
    NSMutableDictionary *parameterDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:parameterStr, @"localdata", nil];
   
    //    NSLog(@"项目概览入参：%@",parameterDic);
    // 发送请求
    dispatch_group_enter(group);
    
    [LSHttpTool Post:URL_PROJECTENGINEERSURVEY_SELEC parameters:parameterDic success:^(id responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"1"]) {
            NSArray *arr=responseObject[@"data"];
            for (NSDictionary *dic in arr) {
                if([self serverDataCompareLocalDataTable:PRO_ENGINEER_SURVEY Data:dic]){
                    [self.projectEngineerSupveyArr addObject:dic];
                }
            }
            dispatch_group_leave(group);
            
        } else {
            NSLog(@"返回项目概况错误：%@",responseObject);
            dispatch_group_leave(group);
            
        }
    } failure:^(NSError *error) {
        NSLog(@"请求项目概况超时");
        dispatch_group_leave(group);
        
    }];
}



- (void)queryProcessFromServe {
    NSString *json = [self getLocalIDAndModifyTimeFrom:PROCESS];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:json forKey:@"localdata"];
    dispatch_group_enter(group);
    
    [LSHttpTool Post:URL_PROCESS_SELECT parameters:dic success:^(id responseObject) {
        NSLog(@"流程返回:%@",responseObject);
        
        if ([responseObject[@"code"] isEqualToString:@"1"]){
            NSArray *arr = responseObject[@"data"];
            // 同步本地数据库
            for (NSDictionary *dic in arr) {
                if([self serverDataCompareLocalDataTable:PROCESS Data:dic]){
                    [self.processArr addObject:dic];
                }
            }
            dispatch_group_leave(group);
            
        } else {
            NSLog(@"查询流程错误1232:%@",responseObject);
            dispatch_group_leave(group);
            
        }
    } failure:^(NSError *error) {
        NSLog(@"查询流程请求超时");
        dispatch_group_leave(group);
        
    }];
}


- (void)queryProcessRecordFromServe{
    
    NSArray *processIDArr = [self getLocalID:@"ID" fromTable:PROCESS];
    
    for (int i = 0; i < processIDArr.count; i++) {
        
        if(i==processIDArr.count-1){
            // 从本地获取流程记录ID
            NSString *jsonStr = [self getLocalIDAndModifyTimeFrom:PROCESS_RECORD where:@"PROCESS_ID" equal:processIDArr[i]];
            
            // 拼接上传参数
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:jsonStr forKey:@"localdata"];
            [dic setObject:processIDArr[i] forKey:@"processId"];
            
            //            NSLog(@"流程记录入参：%@",dic);
            // 向服务器查询数据
            dispatch_group_enter(group);
            
            [LSHttpTool Post:URL_PROCESSRECORD_SELECT parameters:dic success:^(id responseObject) {
                
                //                NSLog(@"流程记录：%@",responseObject);
                
                if ([responseObject[@"code"] isEqualToString:@"1"]){
                    NSArray *flowRecordArr = responseObject[@"data"];
                    
                    for (int j = 0; j < flowRecordArr.count; j++) {
                        NSDictionary *dic = flowRecordArr[j];
                        //                        NSLog(@"processRecord：%@",dic);
                        
                        if([self serverDataCompareLocalDataTable:PROCESS_RECORD Data:dic]){
                            [self.processRecordArr addObject:dic];
                        }
                    }
                    
                } else {
                    NSLog(@"查询流程记录错误");
                    
                }
                
                dispatch_group_leave(group);
                
            } failure:^(NSError *error) {
                dispatch_group_leave(group);
                
            }];
            
        }
        
    }
}


- (void)querySampleProcessFromServe {
    NSString *json = [self getLocalIDAndModifyTimeFrom:SAMPLE_PROCESS];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:json forKey:@"localdata"];
    dispatch_group_enter(group);
    
    [LSHttpTool Post:URL_SAMPLE_PROCESS_GET parameters:dic success:^(id responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"1"]){
            //            NSLog(@"查询见证取样流程正确");
            
            NSArray *arr = responseObject[@"data"];
            // 同步本地数据库
            for (int i = 0; i < arr.count; i++) {
                NSDictionary *dic = arr[i];
                if([self serverDataCompareLocalDataTable:SAMPLE_PROCESS Data:dic]){
                    [self.sampleProcessArr addObject:dic];
                }
            }
            
        } else {
            NSLog(@"查询见证取样流程错误");
        }
        dispatch_group_leave(group);
        
    } failure:^(NSError *error) {
        NSLog(@"查询见证取样流程请求超时");
        dispatch_group_leave(group);
        
    }];
}


- (void)querySampleProcessRecordFromServe{
    
    NSArray *processIDArr = [self getLocalID:@"ID" fromTable:SAMPLE_PROCESS];
    
    for (int i = 0; i < processIDArr.count; i++) {
        
        // 从本地获取见证取样流程记录ID
        NSString *jsonStr = [self getLocalIDAndModifyTimeFrom:SAMPLE_PROCESS_RECORD where:@"PROCESS_ID" equal:processIDArr[i]];
        
        // 拼接上传参数
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:jsonStr forKey:@"localdata"];
        [dic setObject:processIDArr[i] forKey:@"sampleProcessId"];
        
        dispatch_group_enter(group);
        
        // 向服务器查询数据
        [LSHttpTool Post:URL_SAMPLE_PROCESS_RECORD_GET parameters:dic success:^(id responseObject) {
            if ([responseObject[@"code"] isEqualToString:@"1"]){
                
                NSLog(@"查询见证取样流程记录正确");
                NSArray *flowRecordArr = responseObject[@"data"];
                for (id obj in flowRecordArr) {
                    if([self serverDataCompareLocalDataTable:SAMPLE_PROCESS_RECORD Data:obj]){
                        [self.sampleProcessRecordArr addObject:obj];
                    }
                }
                
            } else {
                NSLog(@"查询见证取样流程记录错误");
            }
            dispatch_group_leave(group);
            
        } failure:^(NSError *error) {
            NSLog(@"查询见证取样流程记录请求超时");
            dispatch_group_leave(group);
            
        }];
    }
}



#pragma  mark - 以下方法为传参辅助方法

// 获取本地数据ID和最后修改时间(array)
- (NSArray *)getLocalIDAndModifyTimeArrFrom:(NSString *)tableName {
    // 获取数据库
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    // 打开数据库
    [db open];
    // 准备sql语句
    NSString *sql = [NSString stringWithFormat:@"SELECT ID, MODIFY_TIME FROM %@ WHERE LOCAL_MODIFY_STATE NOT IN('ADD','MOD')", tableName];
    // 查找数据
    FMResultSet *result = [db executeQuery:sql];
    NSMutableArray *array = [NSMutableArray array];
    while ([result next]) {
        NSString *ID = [result stringForColumn:@"ID"];
        if (!ID) {
            continue;
        }
        NSString *modifyTime = DEFISNUL([result stringForColumn:@"MODIFY_TIME"]);
        NSDictionary *pragameDic=@{@"id":ID, @"modifyTime":modifyTime};
        [array addObject:pragameDic];
    }
    [db close];
    return array;
}
// 条件获取本地指定数据
- (NSArray *)getLocalID:(NSString *)localData fromTable:(NSString *)table where:(NSString *)condition equal: (NSString *)equal {
    // 获取数据库
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    // 打开数据库
    [db open];
    // 准备sql语句
    NSString *sql = [NSString stringWithFormat:@"SELECT DISTINCT %@ FROM %@ WHERE %@ = '%@'", localData, table, condition, equal];
    // 查找数据
    FMResultSet *result = [db executeQuery:sql];
    NSMutableArray *array = [NSMutableArray array];
    while ([result next]) {
        NSString *dataStr = DEFISNUL([result stringForColumn:localData]);
        [array addObject:dataStr];
    }
    [db close];
    return array;
}
// 获取本地数据ID和最后修改时间(jsonStr)
- (NSString *)getLocalIDAndModifyTimeFrom:(NSString *)tableName {
    // 获取数据库
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    // 打开数据库
    [db open];
    // 准备sql语句
    NSString *sql = [NSString stringWithFormat:@"SELECT ID, MODIFY_TIME FROM %@ WHERE LOCAL_MODIFY_STATE NOT IN('ADD','MOD')", tableName];
    // 查找数据
    FMResultSet *result = [db executeQuery:sql];
    NSMutableArray *array = [NSMutableArray array];
    while ([result next]) {
        NSString *ID = [result stringForColumn:@"ID"];
        if (!ID) {
            continue;
        }
        NSString *modifyTime = DEFISNUL([result stringForColumn:@"MODIFY_TIME"]);
        NSDictionary *pragameDic=@{@"id":ID, @"modifyTime":modifyTime};
        [array addObject:pragameDic];
    }
    [db close];
    NSString *jsonStr = [LSUtility toJSONData:array];
    return jsonStr;
}

// 获取本地指定ID
- (NSArray *)getLocalID:(NSString *)localID fromTable:(NSString *)table {
    // 获取数据库
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    // 打开数据库
    [db open];
    // 准备sql语句
    NSString *sql = [NSString stringWithFormat:@"SELECT DISTINCT %@ FROM %@ WHERE LOCAL_MODIFY_STATE NOT IN('ADD','MOD')", localID, table];
    
    // 查找数据
    FMResultSet *result = [db executeQuery:sql];
    NSMutableArray *array = [NSMutableArray array];
    while ([result next]) {
        NSString *ID = [result stringForColumn:localID];
        [array addObject:ID];
    }
    [db close];
    return array;
}

// 条件获取本地ID和最后修改时间
- (NSString *)getLocalIDAndModifyTimeFrom:(NSString *)tableName where:(NSString *)condition equal:(NSString *)equal {
    // 获取数据库
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    // 打开数据库
    if (![db open]) {
        [db open];
    }
    // 准备sql语句
    NSString *sql = [NSString stringWithFormat:@"SELECT ID, MODIFY_TIME FROM %@ WHERE %@ = '%@' AND LOCAL_MODIFY_STATE NOT IN('ADD','MOD')", tableName, condition, equal];
    // 查找数据
    FMResultSet *result = [db executeQuery:sql];
    NSMutableArray *array = [NSMutableArray array];
    
    while ([result next]) {
        NSString *ID = [result stringForColumn:@"ID"];
        if ([ID isEqualToString:@""]) {
            continue;
        }
        NSString *modifyTime = DEFISNUL([result stringForColumn:@"MODIFY_TIME"]);
        NSDictionary *pragameDic = @{@"id":ID, @"modifyTime":modifyTime};
        [array addObject:pragameDic];
    }
    
    NSString *jsonStr = [LSUtility toJSONData:array];
    [db close];
    return jsonStr;
    
}

#pragma mark -判断返回的数据 id和本地修改时间是否一样
- (BOOL)serverDataCompareLocalDataTable:(NSString *)table Data:(NSDictionary *)dic{
    NSString *ids=dic[@"id"];
    NSString *modifytime=dic[@"modifytime"];
    
    // 获取数据库
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    // 打开数据库
    if (![db open]) {
        [db open];
    }
    // 准备sql语句
    NSString *sql = [NSString stringWithFormat:@"SELECT ID FROM %@ WHERE ID = '%@' AND MODIFY_TIME ='%@'", table,ids,DEFISNUL(modifytime)];
    // 查找数据
    FMResultSet *result = [db executeQuery:sql];
    NSMutableArray *array = [NSMutableArray array];
    
    while ([result next]) {
        NSString *ID = [result stringForColumn:@"ID"];
        [array addObject:ID];
    }
    //存在和本地一样的数据 返回yes
    if(array.count>0){
        return NO;
    }else{
        return YES;
    }
}

@end
